package leeon.bbsbrower.views;

import leeon.bbsbrower.actions.BBSMailAction;
import leeon.bbsbrower.actions.Config;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class SendMailView extends ViewPart {
	public static final String ID = "bbsbrower.view.sendmail";
	
	private Text id = null;
	private Text title = null;
	private Text mail = null;

	//0 丢小纸条, 1 邮件列表, 2 邮件内容
	private static int en = 0;
	
	//从哪一篇信进入
	private static int m = 0;
	
	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		//布局
		parent.setLayout(null);
				
		id = new Text(parent, SWT.BORDER);		
		id.setBounds(90, 10, 200, 30);		
		id.setFont(ApplicationWorkbenchWindowAdvisor.listFont);		
		id.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//回车到title
				if (e.keyCode == 13) {
					title.setFocus();
					e.doit = false;	
				//esc 退出
				} else if (e.keyCode == SWT.ESC) {
					actionToMail();					
					e.doit = false;					
				}
			}

			public void keyReleased(KeyEvent e) {}
			
		});		
		final Label idLabel = new Label(parent, SWT.NONE);
		idLabel.setText("对方ID:");
		idLabel.setBounds(10, 10, 70, 30);
		idLabel.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		
		title = new Text(parent, SWT.BORDER);
		title.setBounds(90, 50, 900, 30);
		title.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		title.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//回车到mail
				if (e.keyCode == 13) {
					mail.setFocus();
					e.doit = false;
				//退出
				} else if (e.keyCode == SWT.ESC) {
					actionToMail();					
					e.doit = false;					
				//ctrl + w发送
				} else if (e.stateMask == SWT.CTRL && e.keyCode == 119) {
					actionSend();
					e.doit = false;
				}
			}

			public void keyReleased(KeyEvent e) {}
			
		});
		final Label msgLabel = new Label(parent, SWT.NONE);
		msgLabel.setText("标题:");
		msgLabel.setBounds(10, 50, 70, 30);
		msgLabel.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		

		mail = new Text(parent, SWT.BORDER|SWT.V_SCROLL);		
		mail.setBounds(90, 90, 900, 550);	
		mail.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		mail.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//ctrl + w发送
				if (e.stateMask == SWT.CTRL && e.keyCode == 119) {
					actionSend();
					e.doit = false;
				//esc
				} else if (e.keyCode == SWT.ESC) {
					actionToMail();					
					e.doit = false;					
				}
			}

			public void keyReleased(KeyEvent e) {}
			
			
		});
	}
	
	
	/**
	 * 发送的action
	 *
	 */
	private void actionSend() {
		if (id.getText() == null || id.getText().length() == 0) {
			MessageDialog.openWarning(this.getViewSite().getShell(), "信件发送", "对方ID不能为空");
			this.id.setFocus();
			return;
		}
		
		if (title.getText() == null || title.getText().length() == 0) {
			MessageDialog.openWarning(this.getViewSite().getShell(), "信件发送", "标题不能为空");
			this.title.setFocus();
			return;
		}
				
		try {
			BBSMailAction.sendMail(id.getText(), title.getText().replaceFirst(" ", "%20"), Config.MAIL_SIGN, mail.getText(), Config.MAIL_BACKUP);
			actionToMail();
		} catch (Exception e) {
			MessageDialog.openError(this.getViewSite().getShell(), "错误信息", e.toString());			
			e.printStackTrace();
			id.setFocus();
		}
	}
	
	
	/**
	 * 回邮件界面
	 *
	 */
	private void actionToMail() {
		//跳转回到用户操作列表
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			if (en == 0) {			
				MailView m = (MailView)page.showView(MailView.ID);
				m.refreshData(1);
			} else if (en == 1 || en == 2) {
				MailListView m = (MailListView)page.showView(MailListView.ID);
				m.refreshData(-1, SendMailView.m, null);
			}
		} catch (PartInitException e) {
			e.printStackTrace();
		}

	}

	public void refreshData(int en, String id, String title, String mail, int m) {
		if (en != -1) SendMailView.en = en;
		if (id != null) {
			this.id.setText(id);
			this.id.setEditable(false);
		}
		if (title != null) {
			this.title.setText("Re: " + title);
			this.title.setEditable(false);
		}
		if (mail != null) {
			this.mail.setText("\n【 在 "+id+" 的来信中提到: 】\n"+mail);
		}
		SendMailView.m = m;
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		id.setFocus();
	}
}